#include "usbd_core.h"
#include "board.h"
#include <rtdevice.h>

#define LCD_WIDTH 800
#define LCD_HEIGHT 480

#define HID_INT_EP          0x84

#pragma pack(push, 1)
// 触摸点结构体，根据HID描述符定义
typedef struct {
    uint8_t tip_switch : 1;      // 触摸开关状态（0=释放，1=按下）
    uint8_t padding : 7;         // 填充位
    uint8_t contact_id;          // Contact Identifier (Usage 0x51)
    uint16_t x;                  // X坐标（范围0-1024）
    uint16_t y;                  // Y坐标（范围0-576）
    uint16_t width;              // 触摸点宽度 (Usage 0x48)
    uint16_t height;             // 触摸点高度 (Usage 0x49)
} touch_point_t;

// 触摸屏输入报告（报告ID 1），支持5个触摸点
typedef struct {
    uint8_t report_id;           // 报告ID = 1
    touch_point_t points[5];     // 5个触摸点（根据HID描述符）
    uint8_t contact_count;       // 当前有效的触摸点数量 (Usage 0x54)
} touch_input_report_t;

// 特性报告（报告ID 2）
typedef struct {
    uint8_t report_id;           // 报告ID = 2
    uint8_t usage_55;            // Usage (0x55)，范围0-16
} touch_feature_report_2_t;
#pragma pack(pop)

USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX touch_input_report_t report;

// 设置触摸点数据
void set_touch_point(uint8_t point_num, uint8_t tip_switch, uint16_t x, uint16_t y)
{
    // 根据LCD分辨率调整到HID描述符定义的范围
    uint16_t scaled_x = (x * 1024) / LCD_WIDTH;
    uint16_t scaled_y = (y * 576) / LCD_HEIGHT;
    
    report.points[point_num].tip_switch = tip_switch;
    report.points[point_num].padding = 0;
    report.points[point_num].contact_id = point_num;
    report.points[point_num].x = scaled_x;
    report.points[point_num].y = scaled_y;
    report.points[point_num].width  = 0x0030;  // 默认值
    report.points[point_num].height = 0x0030;  // 默认值
}

// 清除所有触摸点
void clear_all_touch_points(void)
{
    for (int i = 0; i < 5; i++) {
        report.points[i].tip_switch = 0;
        report.points[i].padding = 0;
        report.points[i].contact_id = i;
        report.points[i].x = 0;
        report.points[i].y = 0;
        report.points[i].width = 0;
        report.points[i].height = 0;
    }
    report.contact_count = 0;
}

static void app_touch_task(void *arg)
{
    while(usb_device_is_configured(0) == false) {
        rt_thread_mdelay(100);
    }    
    
    // 初始化报告
    rt_memset(&report, 0, sizeof(touch_input_report_t));
    
    while (1)
    {
        // 模拟触摸点移动
        for(int i=0; i<40; i++){
            report.report_id = 1;  // 输入报告ID
            
            // 设置3个触摸点
            set_touch_point(0, 0x01, LCD_WIDTH/2+i*5, LCD_HEIGHT/2);
            // set_touch_point(1, 0x01, LCD_WIDTH/2+i*10, LCD_HEIGHT/2+100);
            // set_touch_point(2, 0x01, LCD_WIDTH/2+i*10, LCD_HEIGHT/2+200);
            
            // 设置触摸点数量
            report.contact_count = 1;  // 3个触摸点

            usbd_ep_start_write(0, HID_INT_EP, (const uint8_t *)&report, sizeof(touch_input_report_t));
            rt_thread_mdelay(20);
        }

        // 清除所有触摸点
        report.report_id = 1;
        clear_all_touch_points();
        usbd_ep_start_write(0, HID_INT_EP, (const uint8_t *)&report, sizeof(touch_input_report_t));
        rt_thread_mdelay(1000);   
    }
}

int app_touch_init(void)
{
    rt_thread_t touch_thread = rt_thread_create("app_touch_task", app_touch_task, RT_NULL, 4096, 15, 20);
    if (touch_thread != RT_NULL) {
        rt_thread_startup(touch_thread);
    }
    return 0;
}

INIT_APP_EXPORT(app_touch_init);